package mypetstore.persistence.impl;

import mypetstore.domain.Category;
import mypetstore.domain.Product;
import mypetstore.persistence.DBUtil;
import mypetstore.persistence.ProductDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {

    private static String getProductListByCategoryString="SELECT PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId FROM PRODUCT WHERE CATEGORY = ?";
    private static String getProductString="SELECT PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId FROM PRODUCT WHERE PRODUCTID = ?";
    private static String searchProductListString="SELECT PRODUCTID,NAME,DESCN as description,CATEGORY as categoryId FROM PRODUCT WHERE lower(name) like ?";
    @Override
    public List<Product> getProductListByCategory(String categoryId) {
        List<Product> productList=new ArrayList<Product>();
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement statement=connection.prepareStatement(getProductListByCategoryString);
            statement.setString(1,categoryId);
            ResultSet resultSet=statement.executeQuery();
            while(resultSet.next()){
                Product product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));
                productList.add(product);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }
        return productList;
    }

    @Override
    public Product getProduct(String productId) {
        Product product=null;
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement statement=connection.prepareStatement(getProductString);
            statement.setString(1,productId);
            ResultSet resultSet=statement.executeQuery();
            if (resultSet.next()){
                product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public List<Product> searchProductList(String keywords) {
        List<Product> productList=new ArrayList<Product>();
        try{
            Connection connection= DBUtil.getConnection();
            PreparedStatement statement=connection.prepareStatement(searchProductListString);
            statement.setString(1,keywords);
            ResultSet resultSet=statement.executeQuery();
            while(resultSet.next()){
                Product product=new Product();
                product.setProductId(resultSet.getString("PRODUCTID"));
                product.setCategoryId(resultSet.getString("categoryId"));
                product.setName(resultSet.getString("NAME"));
                product.setDescription(resultSet.getString("description"));
                productList.add(product);
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch(Exception e){
            e.printStackTrace();
        }
        return productList;
    }
}
